//
//  InteractionViewController.m
//  HONGQIYUN
//
//  Created by 余海华 on 2016/12/19.
//  Copyright © 2016年 Haidi. All rights reserved.
//

#import "InteractionViewController.h"

/// view
#import "HQYSearchControlView.h"
#import "HQYJingMingInteractionCell.h"

/// model
#import "IHomeClient.h"

const static CGFloat kSearControlViewHeight = 40.f;
static NSString *kInteractionCellIndentifier = @"kInteractionCellIndentifier";

const static CGFloat kInteractionCellIHeight = 100.f;


@interface InteractionViewController () <UITableViewDelegate,UITableViewDataSource>

@property (nonatomic, strong) UITableView *myTableView;


@property (nonatomic, strong)HQYSearchControlView *searchControlView;

@property (nonatomic, strong)InteractionInfo *interactionInfo;

@property (nonatomic, strong) NSArray *dataArray;

@property (nonatomic, strong) UIBarButtonItem *rightBarBtnItem;

@property (nonatomic, copy) NSString *curPage;

@property (nonatomic, copy) NSString *pageSize;



@end

@implementation InteractionViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.

}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    
    if(!self.interactionInfo) {
        [self loadData];
    }
}

- (void)loadView {
    [super loadView];
    
    [self.searchControlView setFrame:CGRectMake(10, 10, ScreenWidth - 20, kSearControlViewHeight)];
    [self.view addSubview:self.searchControlView];
    
    
    [self.myTableView setFrame:CGRectMake(0, self.searchControlView.bottom + 10, ScreenWidth, kScreenHeight - 64 - 49 - self.searchControlView.bottom - 10)];
    [self.view addSubview:self.myTableView];
    
    self.navigationItem.rightBarButtonItem = self.rightBarBtnItem;
}

- (void)loadData {
    [self showHudInView:self.view hint:kLoadingText];
    
    self.curPage = @"1";
    [GetCoreI(IHomeClient) requestInteractionListWith:@"" andPage:self.curPage andPageSize:self.pageSize andKeywords:@"" SuccBlock:^(InteractionInfo *interactionInfo) {
        
        self.interactionInfo = interactionInfo;
        InteractionObject *testModel = [[InteractionObject alloc]init];
        testModel.reply_content = @"克制，不要轻举妄动!";
        testModel.title = @"测试数据标题1";
        
        self.dataArray = @[testModel];
        [self.myTableView reloadData];
        
        [self hideHud];
    } failBlock:^(NSString *msg) {
        
        [self hideHud];
    }];
}

#pragma mark - UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    HQYJingMingInteractionCell *cell = [tableView dequeueReusableCellWithIdentifier:kInteractionCellIndentifier];
    
    if(cell == nil) {
        cell = [[HQYJingMingInteractionCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:kInteractionCellIndentifier];
        [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
    }
    
    if(indexPath.row < self.dataArray.count) {
        cell.interactionObject = self.dataArray[indexPath.row];
    }
    
    return cell;
}

#pragma mark - UITableViewDelegate
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return kInteractionCellIHeight;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
}

#pragma mark - 触发事件
- (void)addQuestionAction {
    
}

#pragma mark -lazy load
- (HQYSearchControlView *)searchControlView {
    if(!_searchControlView) {
        _searchControlView = [[HQYSearchControlView alloc]initWithFrame:CGRectZero andPlaceholderText:@"请输入法律类型,如:交通事故"];
    }
    return _searchControlView;
}

- (UIBarButtonItem *)rightBarBtnItem {
    if(!_rightBarBtnItem) {
        UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
        [btn setFrame:CGRectMake(0, 0, 44, 44)];
        [btn addTarget:self action:@selector(addQuestionAction) forControlEvents:UIControlEventTouchUpInside];
        [btn setTitle:@"+" forState:UIControlStateNormal];
        btn.titleLabel.font = [UIFont systemFontOfSize:30];
        _rightBarBtnItem = [[UIBarButtonItem alloc]initWithCustomView:btn];
    }
    return _rightBarBtnItem;
}

- (UITableView *)myTableView {
    if(!_myTableView) {
        _myTableView = [[UITableView alloc]initWithFrame:CGRectZero style:UITableViewStylePlain];
        _myTableView.delegate = self;
        _myTableView.dataSource = self;
    }
    return _myTableView;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
